package com.infinit.easyreader.adpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.infinit.easyreader.R;
import com.infinit.easyreader.entity.Book;

import java.util.List;

/**
 * 类：BooksAdapter [V 1.0.0]
 * 包含类 : com.infinit.easyreader.adpter.BooksAdapter,ViewHolder
 * 作用：书架GridView的适配器
 * Created by rzy on 2016/1/30.
 */
public class BooksAdapter extends BaseAdapter<Book> {
    public BooksAdapter(List<Book> dataList, Context context) {
        super(dataList, context);
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Book book = (Book) getItem(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = getInflater().inflate(R.layout.books_item, null);
            //convertView.setLayoutParams(new GridView.LayoutParams(130,180));
            holder.bookName = (TextView) convertView.findViewById(R.id.tvBookItemName);
            holder.tvCollected = (TextView) convertView.findViewById(R.id.tvCollected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String bookName = book.getName();
        bookName = bookName.substring(0, bookName.lastIndexOf("."));
        holder.bookName.setText(bookName);
        holder.tvCollected.setVisibility(View.GONE);
        if (book.isFavorite()) {
            holder.tvCollected.setVisibility(View.VISIBLE);
        }
        return convertView;
    }


    /**
     * 类名：ViewHolder
     * 预留RelativeLayout设置封面
     */
    private class ViewHolder {
        /**
         * 封面
         */
        @SuppressWarnings("unused")
        RelativeLayout rlCover;
        /**
         * 书名
         */
        TextView bookName;
        //收藏标志
        TextView tvCollected;

    }


}
